#include "timer_schedule.h"
#include "evos_app.h"
#include "drv_init.h"

typedef struct eos_timer_schedule_tag {
    eos_sm_t super;
} eos_timer_schedule_t;
static eos_timer_schedule_t timer_schedule;

// 初始状态
static eos_ret_t state_init(eos_timer_schedule_t *const me, eos_event_t const *const e);
static eos_ret_t state_timer_schedule_handle(eos_timer_schedule_t *const me, eos_event_t const *const e);

void eos_timer_schedule_init(void)
{
    static eos_u32_t queue[4];                    // 事件队列
    eos_sm_init(&timer_schedule.super, 2, queue); // 状态机初始化
                                                  // 状态机启动，以state_init作为初始状态。
    eos_sm_start(&timer_schedule.super, EOS_STATE_CAST(state_init));
}

static eos_ret_t state_init(eos_timer_schedule_t *const me, eos_event_t const *const e)
{
    // 订阅事件
    EOS_EVENT_SUB(Event_Time_10ms);
    EOS_EVENT_SUB(Event_Time_20ms);
    // 使事件Event_Time_10ms，每隔10ms就被发送一次。
    eos_event_pub_period(Event_Time_10ms, 10);
    eos_event_pub_period(Event_Time_20ms, 20);

    return EOS_TRAN(state_timer_schedule_handle);
}
uint8_t u1_test_flag=0;
static eos_ret_t state_timer_schedule_handle(eos_timer_schedule_t *const me, eos_event_t const *const e)
{
    switch (e->topic) {
        case Event_Time_10ms:
            //软件触发ADC采样
            ADC_SoftwareStartConvCmd(ADC1, ENABLE);
            return EOS_TRAN(state_timer_schedule_handle);
        case Event_Time_20ms:
            if(u1_test_flag==1){
                eos_event_pub_topic(Event_1);
            }else if(u1_test_flag==2){
                eos_event_pub_topic(Event_2);
            }
            return EOS_TRAN(state_timer_schedule_handle);
        default:
            return EOS_SUPER(eos_state_top);
    }
}
